<?php

namespace App\Console\Commands\Controller;

use App\Common\Libs\ArrayHelper;
use App\Console\BaseCommand;
use App\Consts\GlobalConst;
use App\Consts\GlobalCounterConst;
use App\Models\Cache\Cache;
use App\Models\Cache\CacheKey;
use App\Models\UserCollect;
use App\Models\UserCollectPackage;

class PackageCommand extends BaseCommand
{
    protected $signature = 'controller:package {action} {json}';

    protected $description = 'package commands';

    /**
     * 删除收藏夹后，异步更新收藏夹内容的收藏数
     * @param $params
     * @return bool
     */
    public function syncTargetCount($params)
    {
        // 更新收藏夹的内容数为0
        UserCollectPackage::getModel($params['user_id'])
            ->where('collect_package_id', $params['collect_package_id'])
            ->update(['total' => 0]);

        // 获取用户所有此收藏夹的收藏
        $collects = UserCollect::getModel($params['user_id'])->where([
            'user_id' => $params['user_id'],
            'package_id' => $params['collect_package_id'],
            'is_deleted' => GlobalConst::IS_NOT_DELETE,
        ])->get();

        $targetTypeCollects = ArrayHelper::index($collects, 'target_type');

        // 更新用户的收藏数
        Cache::getInstance([
            'user_id' => $params['user_id'],
        ])->incrFromArray(
            CacheKey::COUNTER_USER,
            GlobalCounterConst::COUNT_COLLECT,
            -count($collects)
        );

        /**
         * 1. 更新收藏内容的被收藏数
         * 2. 记录取消收藏的日志
         */
        $collect = [];
        foreach ($targetTypeCollects as $targetType => $collectArr) {
            if ($targetType == GlobalConst::TARGET_TYPE_ARTICLE) {
                foreach ($collectArr as $collect) {
                    $collect->delete();
                    Cache::getInstance([
                        'article_id' => $collect->target_id,
                    ])->incrFromArray(
                        CacheKey::COUNTER_ARTICLE,
                        GlobalCounterConst::COUNT_COLLECT,
                        GlobalCounterConst::DECREASE
                    );
                }
            }

            if ($targetType == GlobalConst::TARGET_TYPE_NOTE) {
                foreach ($collectArr as $collect) {
                    $collect->delete();
                    Cache::getInstance([
                        'note_id' => $collect->target_id
                    ])->incrFromArray(
                        CacheKey::COUNTER_NOTE,
                        GlobalCounterConst::COUNT_COLLECT,
                        GlobalCounterConst::DECREASE
                    );
                }
            }

            $data = [
                'send_user_id' => $collect->user_id,
                'receive_user_id' => $collect->user_id,
                'action_id' => $collect->package_id,
                'action_type' => GlobalCounterConst::ACTION_TYPE_CANCEL_COLLECT,
                'target_id' => $collect->target_id,
                'target_type' => $collect->target_type,
                'created_at' => time(),
            ];

            service()->LogCountService->createLogByQueue($data);
        }

        return true;
    }

}
